package com.blog.effective.note6;

/**
 * 存在过期对象引用的队列.
 *
 * @author 张俊强~.
 * @date 2017/11/12-8:48.
 */
@SuppressWarnings("unchecked")
public class MyQueue<T> {

    private T[] elements;   //保存数据的对象数组
    private int size = 0;   //当前队列的大小
    private int cur = 0;    //标识下一个pop出来的对象的位置
    private static final int MyQueueSize = 20;//队列的大小

    public MyQueue() {
        elements = (T[]) new Object[MyQueueSize];
    }

    public void push(T t) {
        if (size < MyQueueSize) {
            elements[size++] = t;
        } else {
            throw new RuntimeException("当前队列已满");
        }
    }

    public T pop() {
        if (cur < size) {
            T t= elements[cur];
            elements[cur]=null;     //指向 null
            cur++;
            return t;
        }
        throw new RuntimeException("当前队列为空");
    }

}
